# -*- coding: utf-8 -*-
"""
@Time : 2024/4/4 16:18
@Author : ChenXiaoliang
@Email : middlegod@sina.com
@File : valid_phone.py
"""
import time
import os
import logging

LOG_FORMAT = "%(levelname)s %(asctime)s - %(message)s"
logging.basicConfig(filename='jd_finance.log', level=logging.INFO, filemode='a', format=LOG_FORMAT)
logger = logging.getLogger(__name__)


def valid_phone(driver):
    try:
        btns = driver.find_elements_by_tag_name("button")
        if btns:
            if "使用 手机短信验证码" in [btn.text for btn in btns]:
                for btn in btns:
                    print(btn.text)
                    if btn.text == "使用 手机短信验证码":
                        btn.click()
                        break
            else:
                print("有按钮，但是没有【使用 手机短信验证码】按钮，所以无需手机号验证！")
                return
        else:
            print("没有任何可点击按钮，无需手机号验证！")
            return
    except Exception as err:
        print(f"认证页面没有找到[使用 手机短信验证码]按钮{err}")
        return

    time.sleep(20)

    btns = driver.find_elements_by_tag_name("button")
    for btn in btns:
        print(btn.text)
        if btn.text == "获取短信验证码":
            btn.click()
            logger.info("已点击获取短信验证码，快去设置，60s后开始填入")
            print("已点击获取短信验证码，快去设置，60s后开始填入")
            break

    time.sleep(60)

    sms_input = driver.find_element_by_class_name("field")
    sms_input.send_keys(input("请输入查收到的验证码："))
    #sms_code = os.environ['VERIFY_CODE']
    #print(f"获取到的验证码{sms_code}")
    #logger.info(f"获取到的验证码{sms_code}")
    #sms_input.send_keys(sms_code)
    #print(f"成功填入验证码{sms_code}")
    #logger.info(f"成功填入验证码{sms_code}")
    time.sleep(5)

    btns = driver.find_elements_by_tag_name("button")
    for btn in btns:
        print("提交认证寻找目标按钮：", btn.text)
        if btn.text == "提交认证":
            btn.click()
            logger.info("已点击提交认证")
            print("已点击提交认证")
            break
